package org.bookie.facebook.pages;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.bookie.framework.CommonBasePage;
import org.bookie.framework.components.IMenuListener;
import org.bookie.model.Category;
import org.bookie.model.services.CategoryDAO;

/**
 * Main application page
 * 
 * @author igor
 *
 */
public abstract class Home extends CommonBasePage implements PageBeginRenderListener {

	private static final Log log = LogFactory.getLog(Home.class);
	
	public static final String BET_TABLE = "betList";

	/**
	 * Get the available betting categories
	 * 
	 * @return instance of Collection
	 */
	public abstract Collection getCategories();

	/**
	 * Set the available betting categories
	 * 
	 * @param cats
	 *            categories to set
	 */
	public abstract void setCategories(Collection cats);
	
	/**
	 * Injected category service.
	 * 
	 * @return instance of CategoryDAO
	 */
	public abstract CategoryDAO getCategoryDAO();


	/**
	 * Getter for the parameter
	 * 
	 * @return
	 */
	public abstract IMenuListener getMenuListener();


	/**
	 * @see org.apache.tapestry.event.PageBeginRenderListener#pageBeginRender(org.apache.tapestry.event.PageEvent)
	 */
	public void pageBeginRender(PageEvent event) {
		if (null == getCategories()) {
			setCategories(getCategoryDAO().findAll());
		}
	}
	
	
	/**
	 * Listener method to select the betting category
	 * 
	 * @param id
	 *            identifier of the category to select
	 */
	public void selectCategory(Long id) {
		Category selectedCategory = getCategoryDAO().findById(id);
		if (selectedCategory == null) {
			log.warn("User have selected unexisting category with ID=" + id);
			return;
		}
		if (getMenuListener() != null) {
			getMenuListener().categorySelected(selectedCategory);
		} else {
			log.error("User is trying to select something from "
					+ "menu but menu listener is not registered");
		}
	}
	
}